Questions
33 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
33 / 50

How can you use :not() effectively to exclude elements from styling?

Using the :not() Pseudo-Class in CSS

The :not() pseudo-class in CSS allows you to exclude specific elements from a set of selectors. This helps you apply styles broadly while intentionally leaving out certain elements.

How :not() Works
  1. 1

    :not(selector) selects all elements that do not match the specified selector.

  2. 2

    You can combine :not() with other pseudo-classes and type selectors for precise targeting.

  3. 3

    It is especially useful to reduce redundant classes or prevent overriding styles unintentionally.

Example: Using :not() to Exclude Elements

In this example, all buttons except the disabled one are styled with a blue background and white text. The disabled button is excluded from the hover and base styles automatically using :not(:disabled).

Best Practices
  1. 1

    Use :not() to simplify CSS and avoid extra classes for exclusions.

  2. 2

    Combine :not() with pseudo-classes like :hover, :focus, or :checked for dynamic styling.

  3. 3

    Avoid overly complex nested :not() expressions, as they may reduce readability and performance.

  4. 4

    Test across browsers to ensure consistent exclusion behavior.

Difficulty: 6/10
Topics: CSS specificity, selector performance, dynamic content exclusion

Scenario Questions

0-2 years experience
  1. 1

    You're trying to style all buttons except the ones with class 'disabled', but the disabled buttons are still getting the styles. How would you fix your CSS selector?

  2. 2

    You have a list of cards and want to apply a border to all except the first one. How would you write the CSS using :not()?

  3. 3

    What happens if you write :not(.btn):not(.primary) — does that exclude elements that have either class or both?

2-5 years experience
  1. 1

    A feature team reports that some dynamically loaded modals are getting unintended styles even though they have a 'modal' class — your CSS uses :not(.modal) on a parent container. What could be going wrong, and how would you debug it?

  2. 2

    You're refactoring a legacy UI and notice that :not([data-hidden]) is applied to hundreds of elements. The page feels sluggish on mobile. What performance concerns might this introduce, and how would you optimize it?

  3. 3

    A designer wants all form inputs styled except those inside a 'readonly-section'. But the section is added dynamically via JS. Why might :not(.readonly-section input) fail, and what’s a more robust approach?

5-8 years experience
  1. 1

    You're designing a reusable component library where :not() is used to exclude variants from base styles. How do you ensure this doesn't create specificity wars when consumers override styles, and what alternatives would you consider?

  2. 2

    In a large-scale app with 10k+ DOM nodes, you're using :not([data-state='loading']) on every list item. How would you evaluate the performance impact, and what architectural changes might you propose?

  3. 3

    A team uses :not() extensively to avoid CSS class bloat, but now their stylesheet is hard to maintain. How would you balance the tradeoff between semantic cleanliness and selector complexity in a scalable design system?

8+ years experience
  1. 1

    You're leading a migration from a legacy CSS framework that uses inline styles and JS-based exclusions to a modern CSS-in-JS system. How would you handle the deprecation of widespread :not() usage without breaking existing layouts or introducing performance regressions?

  2. 2

    Your company's design system uses :not() to conditionally apply theme tokens across components. As you scale to 50+ products with different theming rules, how do you architect this to avoid combinatorial explosion and ensure consistent behavior across teams?

  3. 3

    A cross-team initiative wants to standardize CSS exclusion logic. Some teams use :not(), others use !important overrides or JS classes. How would you design a governance model and migration path that balances flexibility, performance, and long-term maintainability?

Follow-up Questions

  • What happens if you nest :not() inside another negated selector?
  • How would you debug a case where :not() isn't excluding the element you expected?
  • Could this selector cause issues with SSR or hydration in a framework like React?